class Admin::AdvertisementsController < AdminController
  def index
    @advertisements = Advertisement.order("created_at ASC").page(params[:page]).per(params[:per])
  end

  def new
    @advertisement = Advertisement.new
  end

  def create
    @advertisement = Advertisement.new advertisement_params
    if @advertisement.save
      redirect_to admin_advertisements_path, notice: "保存成功！"
    else
      redirect_to admin_advertisements_path, notice: "保存失败！"
    end
  end

  def edit
    @advertisement = Advertisement.find params[:id]
  end

  def update
    @advertisement = Advertisement.find params[:id]
    if @advertisement.update advertisement_params
      redirect_to admin_advertisements_path, notice: "更新成功！"
    else
      redirect_to admin_advertisements_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @advertisement = Advertisement.find params[:id]
    if @advertisement.destroy
      redirect_to admin_advertisements_path, notice: "删除成功."
    else
      redirect_to admin_advertisements_path, notice: "删除失败."
    end
  end

  private

  def advertisement_params
    params.require(:advertisement).permit(:image, :page_url, :page_type, :params, :page, :position)
  end
end
